package cn.mymall.web.controller.houtai.mall;

import cn.mymall.constant.MallConstant;
import cn.mymall.entity.mall.Product;
import cn.mymall.entity.mall.Sku;
import cn.mymall.service.mall.IProductService;
import cn.mymall.service.mall.ISkuService;
import cn.mymall.support.ResultCode;
import cn.mymall.web.controller.houtai.BaseHoutaiController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SkuApiController extends BaseHoutaiController {
	@Autowired
	private ISkuService skuService;
	@Autowired
	private IProductService productService;

	// 列表
	@RequestMapping("/api/sku/list")
	public ResultCode list(Integer productId) {
		QueryWrapper<Sku> ew = new QueryWrapper<>();
		ew.eq("product_id", productId);
		ew.orderByAsc("id");
		List<Sku> list = skuService.list(ew);
		Map map = new HashMap<>();
		map.put("skuList", list);
		return ResultCode.getSuccessReturn(map);
	}

	@RequestMapping("/api/sku/setName")
	public ResultCode setName(Integer id, String name) {
		Sku sku = skuService.getById(id);
		sku.setName(name);
		skuService.updateById(sku);
		return ResultCode.SUCCESS;
	}

	@RequestMapping("/api/sku/setSellPrice")
	public ResultCode setName(Integer id, BigDecimal sellPrice) {
		Sku sku = skuService.getById(id);
		sku.setSellPrice(sellPrice);
		skuService.updateById(sku);
		setProductPrice(sku.getProductId());
		return ResultCode.SUCCESS;
	}

	@RequestMapping("/api/sku/setCostPrice")
	public ResultCode setCostPrice(Integer id, BigDecimal costPrice) {
		Sku sku = skuService.getById(id);
		sku.setCostPrice(costPrice);
		skuService.updateById(sku);
		return ResultCode.SUCCESS;
	}

	@RequestMapping("/api/sku/setInitStock")
	public ResultCode setInitStock(Integer id, Integer initStock) {
		Sku sku = skuService.getById(id);
		sku.setInitStock(initStock);
		skuService.updateById(sku);
		setProductPrice(sku.getProductId());
		return ResultCode.SUCCESS;
	}

	@RequestMapping("/api/sku/setStatus")
	public ResultCode setStatus(Integer id, String status) {
		Sku sku = skuService.getById(id);
		if(StringUtils.equals(status, MallConstant.SKU_STATUS_ON_SALE)){
			if(StringUtils.isBlank(sku.getName()) || sku.getCostPrice()==null || sku.getSellPrice()==null
					|| sku.getInitStock()==null
					|| sku.getInitStock()<=0){
				return ResultCode.getFailure("数据未设置全，不能开放预订");
			}
		}
		sku.setStatus(status);
		skuService.updateById(sku);
		setProductPrice(sku.getProductId());
		return ResultCode.SUCCESS;
	}

	// 保存
	@RequestMapping("/api/sku/save")
	public ResultCode save(Integer productId) {
		Sku sku = new Sku();
		sku.setProductId(productId);
		skuService.saveOrUpdate(sku);
		return ResultCode.SUCCESS;
	}
	private void setProductPrice(Integer productId){
		List<Sku> skuList = skuService.findBookingByProductId(productId);
		BigDecimal price = null;
		for(Sku sku : skuList){
			if(price==null){
				price = sku.getSellPrice();
			}else if(price.compareTo(sku.getSellPrice())==1) {
				price = sku.getSellPrice();
			}
		}
		Product product = productService.getById(productId);
		product.setPrice(price);
		if(price==null){
			product.setStatus(MallConstant.PRODUCT_STATUS_OFF_SALE);
		}
		productService.updateById(product);
	}
}
